package fr.toto.settings.defaultPanels;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JScrollPane;

public class PanelListString extends JPanel implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5356046249757811737L;
	private DefaultListModel<String> listString;
	JTextField addExtField;
	JList<String> extlist;

	public PanelListString(String title, List<String> ls) {
		setAutoscrolls(true);

		listString = new DefaultListModel<String>();
		if (ls != null)
			for (String p : ls) {
				listString.addElement(p);
			}

		this.setMinimumSize(new Dimension(60, 110));
		this.setMaximumSize(new Dimension(32767, 110));
		this.setLayout(null);

		JLabel lblExtentionsVidosAccepts = new JLabel(title);
		lblExtentionsVidosAccepts.setBounds(0, 10, 220, 15);
		lblExtentionsVidosAccepts.setAlignmentY(Component.TOP_ALIGNMENT);
		this.add(lblExtentionsVidosAccepts);

		addExtField = new JTextField();
		addExtField.setBounds(20, 25, 70, 20);
		this.add(addExtField);
		addExtField.setColumns(5);

		JButton addExt = new JButton("+");
		addExt.setMargin(new Insets(0, 0, 0, 0));
		addExt.setBounds(95, 25, 20, 20);
		addExt.setActionCommand("ADD");
		addExt.addActionListener(this);
		this.add(addExt);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(20, 45, 70, 60);
		add(scrollPane);

		extlist = new JList<String>(listString);
		scrollPane.setViewportView(extlist);

		JButton removeExt = new JButton("-");
		removeExt.setMargin(new Insets(0, 0, 0, 0));
		removeExt.setBounds(95, 45, 20, 20);
		removeExt.setActionCommand("SUB");
		removeExt.addActionListener(this);
		this.add(removeExt);

	}

	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getActionCommand().equalsIgnoreCase("ADD")) {
			listString.addElement(addExtField.getText());
		} else {
			List<String> selectedValues = extlist.getSelectedValuesList();
			for (String s : selectedValues) {
				listString.removeElement(s);
			}
		}
	}

	public List<String> getList() {
		ArrayList<String> selectedString = new ArrayList<String>();
		for (int i = 0; i < listString.size(); i++) {
			selectedString.add(listString.get(i));
		}
		return selectedString;

	}

}
